package event;

import java.util.*;

/**
 * EQueueProxyFactory represents a class which must return the best proxy based the specific request using a string
 * This operation has implemented using getProxy method which run the operation.
 * EQueueProxyFactory has implemented using Singleton design pattern. There will be only one instance, which will be
 * accessible using the static method 
 * 
 * @see EQueueProxy
 *
 */
public class EQueueProxyFactory {
	static protected EQueueProxyFactory sharedInstance = null;
	
	/**
	 * Static method used to access at the only instance of the class EQueueProxyFactory (Singleton) 
	 * @return The only and initialized instance of EQueueProxyFactory
	 */
	public static synchronized EQueueProxyFactory getInstance()
	{
		if(sharedInstance == null)
			sharedInstance = new EQueueProxyFactory();
		
		return sharedInstance;
	}
	
	/**
	 * Return an initialized instance of the required proxy based the passed string
	 * Format used IP:PORT:PROTOCOLLO
	 * 
	 * @param s String representing the used protocol to exchange messages between nodes
	 * @see event.EQueueProxy
	 * @exception NoSuchElementException
	 * @return An instance of the chosen proxy or null if the choice isn't possible
	 */
	public synchronized EQueueProxy getProxy(String s)
	{
		StringTokenizer tokenizer = new StringTokenizer(s, ":");
		
		try{
			String host 	= tokenizer.nextToken();
			String port 	= tokenizer.nextToken();
			String protocol = tokenizer.nextToken();
		
			if(protocol.equalsIgnoreCase("UDP"))
			{
				return new EQueueProxy(host, port);
			}
		} catch(NoSuchElementException e)
		{
			e.printStackTrace();
		}
		return null;
	}
}
